<?php
namespace api\app\controller;

use api\app\model\UserModel;
use api\app\validate\Vildate;

use cmf\controller\BaseController;
use cmf\controller\RestBaseController;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use think\facade\Filesystem;
use think\Request;
use think\Validate;


class UserController extends   RestBaseController
{
    //注册
    public  function  register()
    {
        $data =input('post.');
        $validate = new Validate();
        $result = $validate->scene('register');
        if(!$result){
            echo $validate->getError();
        }
        $user = UserModel::where('mobile',$data['mobile'])->find();
        if($user){
            echo '该手机号已被注册';
        }


        $check = checkSmsCode($data['mobile'],$data['code']);
        if(!$check){
            return error('验证码错误');
        }
        $randStr = createRandStr();

        $data['salt'] = $randStr;

        $data['create_time'] = time();
        if(UserModel::create($data)){
            $user = UserModel::where('mobile',$data['mobile'])->find();
            $token=signToken($user['id'],$data['mobile']);
            return success('注册成功',$token);
        }else{
            return error('注册失败');
        }
    }
    public function login(){
        $data = input('post.');
        $validate = new Validate();
        $result = $validate->scene('login')->check($data);
        if(!$result){
            return error($validate->getError());
        }
        $user = UserModel::where('mobile',$data['mobile'])->find();
        $token=signToken($user['id'],$data['mobile']);
        if(!$user){
            return error('该账号不存在');
        }
        $check = checkSmsCode($data['mobile'],$data['code']);
        if(!$check){
            return error('验证码错误');
        }

        return success('登录成功',$token);
    }
    public  function  yzm()
    {
        $data = input('post.');
        sendSms($data['mobile']);
        return error('发送成功');
    }
    public function getNewsInfo(Request $request)
    {
        $userid=$request->hello;
        $data = UserModel::where('id','=',$userid)
            ->find();
        return success('查询成功',$data);
    }


    public function editPost(Request $request)
    {
        $userid=$request->hello;
        $data = input('post.');

        $validate = new Vildate();
        $result = $validate->scene('edit')->check($data);
        if (!$result) {
            $this->error($validate->getError());
        }



        $list = UserModel::find($userid);
        if (!$list) {
            $this->error('未查询的记录');
        }
        $list->update_time = date('Y-m-d H:i:s');
        $list->mobile = $data['mobile'];
        $list->image = $data['image'];
        $list->user_name = $data['user_name'];
        if ($list->save()) {
            return success('保存成功',$list);
        } else {
            return error('保存失败');
        }
    }
    public function upload(){
            $image = request()->file('image');
            $image = Filesystem::disk('public')->putFile('image', $image);
            if($image)
            {
                $this ->success('图片地址',
                    ['image'=>request()->domain().'/storage/'.$image]);

        }
    }



}